<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	
	</head>
	<body>
		<form name="instrumentForm" id="instrumentForm">
			<fieldset>
				<legend>
					Describe instrument:
				</legend>
				<table width="100%">
					<tr>
						<td align="left">
							<label>Type:</label>
						</td>
						<td align="right">
							<input dojoType="dijit.form.ValidationTextBox" name="type" id="type" required="true" 	promptMessage="Enter instrument type" invalidMessage="Description is required."/>	
						</td>
					</tr>
					<tr>
						<td align="left">
							<label>Description:</label>
						</td>
						<td align="right">
							<input dojoType="dijit.form.ValidationTextBox" name="description" id="description" required="true" 	promptMessage="Enter instrument description." invalidMessage="Description is required." />
						</td>
					</tr>
					<tr>
						<td align="left">
							<label>Time slot:</label>
						</td>
						<td align="right">
							<input dojoType="dijit.form.ValidationTextBox" name="timeSlot" id="timeSlot" required="true" 	promptMessage="Enter instrument time slot unit" invalidMessage="Time slot is required." regExp="(\d)*"/>
						</td>
					</tr>
					<tr>
						<td align="left">
							<label>Usage price:</label>
						</td>
						<td align="right">
							<input dojoType="dijit.form.TextBox" name="usagePrice" id="usagePrice" required="true" 	promptMessage="Enter instrument usage price per slot." invalidMessage="Price is required." regExp="(\d)*\.(\d)*"/>
						</td>
					</tr>
					<tr>
						<td align="left">
							<label>
								Access level:
							</label>
						</td>
						<td align="right">
							<select dojoType="dijit.form.ComboBox" name="role" id="role" store="rolesStore" searchAttr="role">
							</select>
						</td>
					</tr>
					<tr>						
						<td colspan=2 align="right">
							<input type="hidden" name="instrumentOp" id="instrumentOp" value="" />
							<button dojoType="dijit.form.Button" name="cancelInstrument" id="instCancel">
								Cancel
								<script  type="dojo/method" event="onClick" args="event">
									dijit.byId( "instrumentDialog").destroyRecursive( );
								</script>
							</button>
							<button dojoType="dijit.form.Button" name="cancelSave" id="instSave">
								Save
								<script  type="dojo/method" event="onClick" args="event">
									if ( !dijit.byId( "type") ||
										 !dijit.byId( "description") ||
										 !dijit.byId( "timeSlot") ||
										 !dijit.byId( "usagePrice") ||
										 !dijit.byId( "role") ||
										 dijit.byId( "role").value.length == 0 ||
										 dijit.byId( "usagePrice").value.length == 0 ||
										 dijit.byId( "timeSlot").value.length == 0 ||
										 dijit.byId( "description").value.length == 0 ||
										 dijit.byId( "type").value.length == 0) {
								        dijit.byId('toaster').setContent('All fields must be filled in before submit.', 'fatal');
								        dijit.byId('toaster').show();							
										return;
									}
									var instrumentId = "";
									if ( dojo.byId( "instrumentOp").value != "instrument_insert")
										instrumentId = dijit.byId( "instrumentsGrid").selection.getSelected( )[0].id[0];
									dojo.xhrPost( {
										url 	: "/FinalProject/app/instrumentManagement",
										content : dojo.mixin( dijit.byId( "instrumentDialog").getValues( ), 
															  {instrumentOp:dojo.byId( "instrumentOp").value, id: instrumentId}),
										load 	: function( data, ioArgs) {
											if ( data == "ERROR") {
										        dijit.byId('toaster').setContent('Backend error. Could not insert instrument, please try later.', 'fatal');
										        dijit.byId('toaster').show();							
												return;
											}
											dijit.byId("toaster").setContent( "Instrument was successefuly added.", "fatal");
											dijit.byId("toaster").show( );
											if ( dojo.byId( "instrumentOp").value == "instrument_update") {
												dijit.byId("instrumentsGrid").removeSelectedRows( );
											}
											dijit.byId("instrumentsGrid").store.newItem( dojo.mixin( dijit.byId( "instrumentDialog").getValues( ), { id : data}));
											dijit.byId( "instrumentDialog").destroyRecursive( );
										},
										error	: function( data, ioArgs) {
									        dijit.byId('toaster').setContent('Backend error. Could not insert instrument, please try later.', 'fatal');
									        dijit.byId('toaster').show();							
											dijit.byId( "instrumentDialog").destroyRecursive( );
										} 
									});
								</script>
							</button>
						</td>
					</tr>
				</table>
			</fieldset>
		</form>
	</body>
</html>